package com.hellpro.lolbox.video.model;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.utils.UIManger;
import com.hellpro.lolbox.video.bean.VideoClassifyBean;
import com.hellpro.lolbox.video.view.fragment.view.VideoClassifySecondActivity;

import java.util.List;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/19
 * 备注:分类导航页面的RecyclerView的适配器
 */

public class ClassifyRecyclerViewAdapter extends BaseAdapter {


    private List<VideoClassifyBean> mList;
    private ClassifyItemRecyclerViewAdapter adapter;
    private ClassifyItemRecyclerViewAdapter adapter1;
    private boolean isClick = true;


    public void setData(List<VideoClassifyBean> data, Context context) {
        this.mList = data;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_classify_list_item, parent, false);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }

        final VideoClassifyBean bean = mList.get(position);
        holder.tvName.setText(bean.getName());


        final List<VideoClassifyBean.SubCategoryBean> subCategory = bean.getSubCategory();
        holder.recyclerList.setNumColumns(4);
        holder.recyclerList.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

        holder.gridView2.setVisibility(View.GONE);

        //初始化适配器
        ClassifyItemRecyclerViewAdapter adapter = new ClassifyItemRecyclerViewAdapter();
        if (subCategory.size() <= 8) {
            holder.ivSHow.setVisibility(View.GONE);
//            adapter = new ClassifyItemRecyclerViewAdapter(bean.getSubCategory());
            adapter.setData(bean.getSubCategory());
        } else if (subCategory.size() > 8) {
            holder.ivSHow.setVisibility(View.VISIBLE);

//            adapter = new ClassifyItemRecyclerViewAdapter(subCategory.subList(0, 8));
            adapter.setData(subCategory.subList(0, 8));
            holder.ivSHow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isClick) {
                        if (subCategory.size() > 8) {
                            //TODO：设置条目超过8的剩余数据的显示
                            holder.gridView2.setVisibility(View.VISIBLE);
                            holder.gridView2.setNumColumns(4);
                            holder.gridView2.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
                            //处理数据
//                          adapter1 = new ClassifyItemRecyclerViewAdapter(subCategory.subList(8, subCategory.size()));
                            adapter1 = new ClassifyItemRecyclerViewAdapter();
                            adapter1.setData(subCategory.subList(8, subCategory.size()));
//                          adapter.setData(subCategory.subList(8, subCategory.size()),position);
                            holder.gridView2.setAdapter(adapter1);
                            holder.gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    String tag = subCategory.subList(8, subCategory.size()).get(position).getTag();
                                    String name = subCategory.subList(8, subCategory.size()).get(position).getName();
                                    UIManger.startToSecondActivity(parent.getContext(), VideoClassifySecondActivity.class, tag, name);
                                }
                            });

                        }
                        isClick = false;
                        adapter1.notifyDataSetChanged();
                    } else {
                        adapter1 = null;
                        holder.gridView2.setVisibility(View.GONE);
                        isClick = true;
                    }
                }
            });
        }

        holder.recyclerList.setAdapter(adapter);
        holder.recyclerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String tag = subCategory.get(position).getTag();
                String name = subCategory.get(position).getName();
                UIManger.startToSecondActivity(parent.getContext(), VideoClassifySecondActivity.class, tag, name);
            }
        });

        return convertView;
    }


    /**
     * 类描述:
     * 创建人:   * 创建时间:2016/10/19
     * 备注:初始化控件
     */
    class MyViewHolder {
        private TextView tvName;
        private ImageView ivSHow;
        private GridView recyclerList;
        private GridView gridView2;

        public MyViewHolder(View convertView) {

            tvName = (TextView) convertView.findViewById(R.id.video_classify_item_title);
            ivSHow = (ImageView) convertView.findViewById(R.id.video_classify_ivShow);
            recyclerList = (GridView) convertView.findViewById(R.id.video_classify_item_recyclerView);
            gridView2 = (GridView) convertView.findViewById(R.id.video_classify_item_gridView2);
        }
    }
}
